package modelLayer;
import java.util.ArrayList;

/**
 * A container to the class Person
 * 
 * @author Gruppe E 
 * @version 1.0
 */
public class PersonContainer
{
    // instance variables - replace the example below with your own
    private ArrayList<Person> persons;
    private static PersonContainer instance;

    /**
     * Constructor for objects of class PersonContainer
     */
    private PersonContainer()
    {
        persons = new ArrayList<Person>();
    }

    /**
     * Method getInstance
     *
     *singleton
     *
     * @return The return value
     */
    public static PersonContainer getInstance()
    {
        if(instance == null)
        {
            instance = new PersonContainer();
        }
        return instance;
    }

    /**
     * Method addLoan
     *
     * @param l A parameter
     * @param phone A parameter
     */
    public void addLoan(Loan l, String phone)
    {
        findPerson(phone).addLoan(l);
    }
    
    /**
     * Method getLoans
     *
     * @param phone A parameter
     * @return The return value
     */
    public ArrayList<Loan> getLoans(String phone)
    {
        Person tempP = findPerson(phone);
        return tempP.getLoans();
    }
    
    /**
     * Method addPerson
     * 
     * adds a person P to the arraylist persons
     *
     * @param p A parameter
     * @return The return value
     */
    public void addPerson(Person p)
    {
        //boolean successful = false;
        persons.add(p);
        //         if(persons.contains(p))
        //       {
        //         successful = true;
        //       }
        //return successful;
    }

    /**
     * Method findPerson
     * 
     * This method will search for a phone number, and return the corresponding person
     *
     * @param phone number
     * @return the correct person
     */
    public Person findPerson(String phone)
    {
        Person returnPerson = null;
        boolean found = false;
        int size = persons.size();
        for(int i = 0;  i < size && !found;i++)
        {
            if(persons.get(i).getPhone().equals(phone))
            {
                returnPerson = persons.get(i);
                found = true;
            }
        }
        return returnPerson;
    }

    /**
     * Method deletePerson
     * 
     * deletes a person that matches the given phone number
     *
     * @param phone A parameter
     * @return The return value
     */
    public boolean deletePerson(String phone)
    {
        boolean successful = true;
        if(findPerson(phone)==null)
        {
            successful = false;
        }
        Person tempP = findPerson(phone);
        persons.remove(tempP);

        return successful;
    }

    /**
     * Method updatePerson
     * 
     * searches for the persons phone number, and updates the persons personal information
     *
     * @param oldPhone A parameter
     * @param name A parameter
     * @param address A parameter
     * @param zip A parameter
     * @param city A parameter
     * @param phone A parameter
     * @return The return value
     */
    public boolean updatePerson(String oldPhone, String name, String address, String zip, String city, String phone)
    {
        boolean successful = false;
        Person tempP = findPerson(oldPhone);
        tempP.setName(name);
        tempP.setAddress(address);
        tempP.setZip(zip);
        tempP.setCity(city);
        tempP.setPhone(phone);
        if(tempP.getAddress().equals(address) && tempP.getCity().equals(city) && tempP.getName().equals(name) && tempP.getPhone().equals(phone) && tempP.getZip().equals(zip))
        {
            successful = true;
        }
        return successful;
    }
}
